import {Injectable} from "@angular/core";
import {Operator} from "./domains";
import {HttpHelper} from "./HttpHelper";
import {WorkShop} from "./domains";

@Injectable()
export class OperatorService{
    operator:Operator;
    currentTag:number;
    constructor(private helper:HttpHelper){

    }
    /*deprecated*/
    operators(tag?,page?,size?,store?,workshop?):Promise<Operator[]>{
        let url = `operator/?`;

        if (page && size) {
            url = url + `page=${page}&size=${size}`;
        }
        if (tag) {
            url = url + `&tag=${tag}`;
        }
        if (store) {
            url = url + `&store=${store}`;
        }
        if (workshop) {
            url = url + `&workshop=${workshop}`
        }
       return this.helper.getJson<Operator[]>(url);
    }

    getOperators(page,size,store?,workshop?,tags?): Promise<Operator[]> {
        let url = `operator/?page=${page}`;
        if (page && size){
            url = url + `&size=${size}`;
        }
        if (store) {
            url = url + `&store=${store}`;
        }
        if (workshop) {
            url = url + `&workshop=${workshop}`;
        }
        tags.forEach(tag => {
            url = url + `&tags=${tag}`;
        })
        return this.helper.getJson<Operator[]>(url);
    }


    /*后期弃用*/
    allOperators(tag?,store?):Promise<Operator[]>{
        let url = "operator/all";
        if (tag) {
            url = url +`/?tag=${tag}`;
        }
        if (store) {
            url = url + `/?store=${store}`;
        }
        if (store && tag) {
            url = `operator/all/?tag=${tag}&store=${store}`;
        }
        return this.helper.getJson<Operator[]>(url);
    }

    addOperator(operator:Operator,storeId?,workshops?){
        if (storeId) {
            operator.store.id = storeId;
        } else {
            operator.store = null;
        }
        if (workshops && workshops.length) {
            workshops.forEach(workshop => {
                let work = new WorkShop();
                work.id = workshop;
                operator.workShops.push(work);
            })
        }
        return this.helper.postToJson("operator",operator);
    }


    signIn(user,pass,tag?):Promise<Operator>{
        let urlFrom = `user=${user}&pass=${pass}`;
        if (tag!=null) {
            urlFrom = `user=${user}&pass=${pass}&tag=${tag}`;
        }
        return this.helper.postToText("login",urlFrom,this.helper.contentHeaderEnum.formHeader)
            .then(res=>{
                this.helper.authorization=res;
                return this.helper.getJson<Operator>("operator/logined");
            });
    }

    withTagAuth(tag) {
        let url = `login/?tag=${tag}`;
        return this.helper.getText(url)
            .then(res => {
                this.helper.authorization = res;
                this.currentTag = tag;
            });
    }

    getOperator(id) {
        let url = `operator/${id}`;
        return this.helper.getJson<Operator>(url);
    }

    updateOperator(operator) {
        let url = `operator`;
        return this.helper.putToBoolean(url,operator);
    }

}